﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WPF_UI_Lightningchart.Helper
{
    public class FFTProcessor
    {
        private Queue<float> _dataQueueLeft = new Queue<float>();
        private Queue<float> _dataQueueRight = new Queue<float>();

        private int _fftDataNumOnce; // 每次FFT数据数量
        private int _samplingFrequency; // 采样频率
        private int _intervalFFT; // FFT间隔
        private int _resolution; // FFT窗口长度
        private int _channelCount = 0; // 采样计数
        private int _sampleTimesCount = 0; // 采样计数
        
        private double[][] _fftsamples;
        
        private RealtimeFFTCalculator _calculatorFFT;

        public delegate void FFTResultHandler(double[][][] xValues, double[][][] yValues, double speed);
        public event FFTResultHandler OnFFTCalculated;

        public FFTProcessor(int fftDataNumOnce, int samplingFrequency, int intervalFFT, int resolution, int channelCount)
        {
            _fftDataNumOnce = fftDataNumOnce;
            _samplingFrequency = samplingFrequency;
            _intervalFFT = intervalFFT;
            _resolution = resolution;
            _channelCount = channelCount;

            _calculatorFFT = new RealtimeFFTCalculator(intervalFFT, samplingFrequency, resolution, channelCount);
            _fftsamples = new double[_channelCount][];
            for (int i = 0; i < _channelCount; i++) 
            {
                _fftsamples[i] = new double[intervalFFT * samplingFrequency / 1000];
            }
        }

        public void AddData(float[] leftValue, float[] rightValue, double speed)
        {
            foreach (var value in leftValue)
            {
                _dataQueueLeft.Enqueue(value);
            }
            foreach (var value in rightValue)
            {
                _dataQueueRight.Enqueue(value);
            }

            if (_dataQueueLeft.Count >= _fftDataNumOnce && _dataQueueRight.Count >= _fftDataNumOnce)
            {
                ProcessFFT(speed);
            }
        }

        private void ProcessFFT(double speed)
        {
            float[] frameL = new float[_fftDataNumOnce];
            float[] frameR = new float[_fftDataNumOnce];
            for (int i = 0; i < _fftDataNumOnce; i++)
            {
                int j = _fftDataNumOnce * _sampleTimesCount + i;
                frameL[i] = _dataQueueLeft.Dequeue();
                frameR[i] = _dataQueueRight.Dequeue();
                _fftsamples[0][j] = frameL[i];
                _fftsamples[1][j] = frameR[i];
            }
            _sampleTimesCount++;
            if (_sampleTimesCount * _fftDataNumOnce >= _intervalFFT * _samplingFrequency / 1000)
            {
                _sampleTimesCount = 0;
                var result = CalculateFFT();
                if (result.Item1 != null && result.Item2 != null)
                {
                    OnFFTCalculated?.Invoke(result.Item1, result.Item2, speed);
                }
                _fftsamples[0] = new double[_intervalFFT * _samplingFrequency / 1000];
                _fftsamples[1] = new double[_intervalFFT * _samplingFrequency / 1000];
            }
        }

        private (double[][][], double[][][]) CalculateFFT()
        {
            double[][][] yValues;
            double[][][] xValues;

            //Console.WriteLine("打印 _fftsamples 内容:");

            //for (int i = 0; i < _fftsamples.Length; i++)
            //{
            //    string channelData = string.Join(", ", _fftsamples[i].Select(val => val.ToString("F4")));
            //    Console.WriteLine($"通道 {i + 1} 数据长度{_fftsamples[i].Length} 数据: {channelData}");
            //}

            bool calcResult = _calculatorFFT.FeedDataAndCalculate(_fftsamples, out xValues, out yValues);
            if (calcResult)
            {
                //Console.WriteLine("FFT 计算成功！");
                return (xValues, yValues);
            }
            else
            {
                Console.WriteLine("FFT 计算失败！");
                return (null, null);
            }
        }
    }

}

